package com.ntl.webcore.system.business.utils.monitor;

import com.ntl.webcore.common.exception.CommonExceptionConstant;
import com.ntl.webcore.common.exception.CommonRuntimeException;
import com.ntl.webcore.common.utils.IpUtils;
import com.ntl.webcore.common.utils.ShiroUtils;
import com.ntl.webcore.common.utils.StringUtils;
import com.ntl.webcore.system.business.domain.SysErrorLog;
import com.ntl.webcore.system.business.service.ISysErrorLogService;
import com.ntl.webcore.tools.spring.bean.SpringUtils;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.io.PrintWriter;
import java.io.StringWriter;

@Component("NTL-SysErrorLogService")
public class SysErrorLogService {
    /**
     * 保存错误日志
     * @param errorCode 错误编码
     * @param customMessage 自定义错误消息
     * @param exception 错误异常对象
     */
    public static void insertErrorLog(String errorCode, String customMessage, Exception exception){
        // error Type
        SysErrorLog errorLog = new SysErrorLog();
        errorLog.setErrorCode(errorCode);

        String exceptionMessage = "";
        if(null != exception){
            exceptionMessage = exception.getMessage();
            // NullPointException 是直接获取不到message
            if(StringUtils.isEmpty(exceptionMessage) || exception.getCause() instanceof NullPointerException){
                StringWriter sw = new StringWriter();
                exception.printStackTrace(new PrintWriter(sw,true));
                exceptionMessage = sw.toString();
            }
        }
        StringBuffer errorMessage = new StringBuffer("自定义错误消息：").append(customMessage).append("</br>系统错误消息：").append(exceptionMessage);
        errorLog.setErrorMessage(errorMessage.toString());

        // server IP
        errorLog.setServerIp(IpUtils.getHostIp());
        // service ID
        Environment environment = SpringUtils.getBean(Environment.class);
        errorLog.setServiceId(environment.getProperty("app.id"));
        // operator
        errorLog.setOperator(ShiroUtils.getLoginName());
        try{
            ISysErrorLogService errorLogService = SpringUtils.getBean(ISysErrorLogService.class);
            errorLogService.insertSysErrorLog(errorLog);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 保存错误日志
     * @param errorCode 错误编码
     * @param e Exception
     */
    public static void insertErrorLog(String errorCode,Exception e){
        insertErrorLog(errorCode,null,e);
    }

    /**
     * 保存错误日志
     * @param e Exception
     */
    public static void insertErrorLog(Exception e){
        insertErrorLog(null,null,e);
    }

    /**
     * 保存错误日志
     * @param e CommonRuntimeException
     */
    public static void insertErrorLog(CommonRuntimeException e){
        insertErrorLog(e.getErrorCode(),e.getErrorMessage(),e);
    }

    /**
     * 报错错误日志
     * @param constant CommonExceptionConstant
     * @param e
     */
    public static void insertErrorLog(CommonExceptionConstant constant, Exception e){
        insertErrorLog(constant.getErrorCode(), constant.getErrorMessage(), e);
    }



}
